package br.edu.sistemaacademico.builder;



import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

import br.edu.sistemaacademico.builder.impl.DeleteSQLBuilder;
import br.edu.sistemaacademico.builder.impl.InsertSQLBuilder;
import br.edu.sistemaacademico.builder.impl.SelectSQLBuilder;
import br.edu.sistemaacademico.builder.impl.UpdateSQLBuilder;
import br.edu.sistemaacademico.dao.DaoGenerico;
import br.edu.sistemaacademico.facade.DataFacade;
import br.edu.sistemaacademico.pojo.Professor;
import br.edu.sistemaacademico.pojo.PrototypeFactory;

public class ProfessorDAO extends DaoGenerico {

	public static final List<String> COLUNAS = Arrays.asList(new String[] {
			"codigo", "nome", "cpf", "endereco", "telefone", "sexo", "rg",
			"ctps", "graduacao", "especializacao", "mestrado",
			"doutorado", "dataEmtrada", "dataSaida", "login", "senha","status" });

	DataFacade facade= new DataFacade();
	
	public List<Object> getCampos(){
		return (List) COLUNAS;
	}
	@Override
	public void alterar(Object o) throws SQLException {
		Professor p = (Professor) o;
		List<Object> valores = Arrays.asList(new Object[] { p.getId(),
				p.getNome(), p.getCpf(), p.getEndereco(), p.getTelefone(),
				p.getSexo(), p.getRg(), p.getCtps(),
				p.getGraducao(), p.getEspecializacao(), p.getMestrado(),
				p.getDoutorado(), facade.getStingGravar(p.getAnoEntrada()), facade.getStingGravar(p.getAnosaida()), p.getLogin(), p.getSenha(), p.getStatus() });

		String condicao = "codigo = " + p.getId() + " and status = 1";
		SQLDirector dir = new SQLDirector();
		UpdateSQLBuilder update = new UpdateSQLBuilder();

		update.setNomeTabela("tb_Professor");
		update.setColunas(COLUNAS);
		update.setValores(valores);
		update.setCondicao(condicao);

		dir.buildSQL(update);
		try {
			executeCommand(update.getSQL());
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	@Override
	public Object getObject(String condicao, String tabelasAdicionais) throws SQLException {
		Professor p  = (Professor) PrototypeFactory.createPrototypeFactory("professor");
		SQLDirector dir = new SQLDirector();
		SelectSQLBuilder select = new SelectSQLBuilder();

		select.setNomeTabela("tb_Professor a"+tabelasAdicionais);
		select.setColunas(COLUNAS);
		// select.setValores(valores);
		select.setCondicao(condicao+" and status =1");
		select.colunasToReturn = "a.*";

		dir.buildSQL(select);
		try {
			ResultSet rs = executeQuery(select.getSQL());
			if (rs.next())
				p = (Professor) informacoes(rs);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return p;
	}
	
	public List<Object> getAll(String condicao, String tabelasAdicionais, String ordenacao){
		Professor p  = (Professor) PrototypeFactory.createPrototypeFactory("professor");
		List<Professor> list = new LinkedList<Professor>();
		SQLDirector dir = new SQLDirector();
		SelectSQLBuilder select = new SelectSQLBuilder();

		select.setNomeTabela("tb_Professor a"+tabelasAdicionais);
		select.setColunas(COLUNAS);
		select.setCondicao(condicao+" and status =1");
		select.colunasToReturn = "a.*";
		select.orderBy = ordenacao;
		
		dir.buildSQL(select);
		try {
			ResultSet rs = executeQuery(select.getSQL());
			while(rs.next()){
				list.add((Professor) informacoes(rs));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return (List) list;
	}

	@Override
	public void gravar(Object o) throws SQLException {
		Professor p = (Professor) o;
		
		List<Object>  valores ;
		if(p.getAnosaida()!=null)
		valores = Arrays.asList(new Object[] { p.getId(),
				p.getNome(), p.getCpf(), p.getEndereco(), p.getTelefone(),
				p.getSexo(), p.getRg(), p.getCtps(),
				p.getGraducao(), p.getEspecializacao(), p.getMestrado(),
				p.getDoutorado(), facade.getStingGravar(p.getAnoEntrada()), facade.getStingGravar(p.getAnosaida()), p.getLogin(), p.getSenha(),1 });
		else
			valores = Arrays.asList(new Object[] { p.getId(),
					p.getNome(), p.getCpf(), p.getEndereco(), p.getTelefone(),
					p.getSexo(), p.getRg(), p.getCtps(),
					p.getGraducao(), p.getEspecializacao(), p.getMestrado(),
					p.getDoutorado(), facade.getStingGravar(p.getAnoEntrada()),"Null", p.getLogin(), p.getSenha(),1 });
			
		for(int i =0;i<valores.size();i++)
			System.out.println(valores.get(i));
		SQLDirector dir = new SQLDirector();
		InsertSQLBuilder insert = new InsertSQLBuilder();

		insert.setNomeTabela("tb_Professor");
		insert.setColunas(COLUNAS);
		insert.setValores(valores);

		dir.buildSQL(insert);
		System.out.println(insert.getSQL());
		try {
			executeCommand(insert.getSQL());
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	@Override
	public Object informacoes(ResultSet rs) throws SQLException {
		Professor a  = (Professor) PrototypeFactory.createPrototypeFactory("professor");
		a.setId(rs.getLong(COLUNAS.get(0)));
		a.setNome(rs.getString(COLUNAS.get(1)));
		a.setCpf(rs.getString(COLUNAS.get(2)));
		a.setEndereco(rs.getString(COLUNAS.get(3)));
		a.setTelefone(rs.getString(COLUNAS.get(4)));
		a.setSexo(rs.getString(COLUNAS.get(5)).charAt(0));
		a.setRg(rs.getString(COLUNAS.get(6)));
		a.setCtps(rs.getString(COLUNAS.get(7)));
		a.setGraducao(rs.getString(COLUNAS.get(8)));
		a.setEspecializacao(rs.getString(COLUNAS.get(9)));
		a.setMestrado(rs.getString(COLUNAS.get(10)));
		a.setDoutorado(rs.getString(COLUNAS.get(11)));
		facade.setDataVisao(rs.getString(COLUNAS.get(12)));
		a.setAnoEntrada(rs.getDate(COLUNAS.get(12)));
		facade.setDataVisao(rs.getString(COLUNAS.get(13)));
		a.setAnosaida(rs.getDate(COLUNAS.get(13)));
		a.setLogin(rs.getString(COLUNAS.get(14)));
		a.setSenha(rs.getString(COLUNAS.get(15)));
		a.setStatus(rs.getInt(COLUNAS.get(16)));
		return a;
	}

	@Override
	public void remover(Object o) throws SQLException {
		Professor p = (Professor) o;
		List<Object> valores = Arrays.asList(new Object[] { p.getId(),
				p.getNome(), p.getCpf(), p.getEndereco(), p.getTelefone(),
				p.getSexo(), p.getRg(), p.getCtps(),
				p.getGraducao(), p.getEspecializacao(), p.getMestrado(),
				p.getDoutorado(), facade.getStingGravar(p.getAnoEntrada()), facade.getStingGravar(p.getAnosaida()), p.getLogin(), p.getSenha() });

		SQLDirector dir = new SQLDirector();
		DeleteSQLBuilder delete = new DeleteSQLBuilder();

		delete.setNomeTabela("tb_Professor");
		delete.setColunas(COLUNAS);
		delete.setValores(valores);
		// delete.setCondicao(condicao);

		dir.buildSQL(delete);
		try {
			executeCommand(delete.getSQL());
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}
	public void deletar(Object o) {
		Professor p = (Professor) o;
		List<Object> valores = Arrays.asList(new Object[] { p.getId(),
				p.getNome(), p.getCpf(), p.getEndereco(), p.getTelefone(),
				p.getSexo(), p.getRg(), p.getCtps(),
				p.getGraducao(), p.getEspecializacao(), p.getMestrado(),
				p.getDoutorado(), facade.getStingGravar(p.getAnoEntrada()), facade.getStingGravar(p.getAnosaida()), p.getLogin(), p.getSenha(), 0 });

		String condicao = "codigo = " + p.getId() + " and status=1";
		SQLDirector dir = new SQLDirector();
		UpdateSQLBuilder update = new UpdateSQLBuilder();

		update.setNomeTabela("tb_Professor");
		update.setColunas(COLUNAS);
		update.setValores(valores);
		update.setCondicao(condicao);

		dir.buildSQL(update);
		try {
			executeCommand(update.getSQL());
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	

}
